<?php

namespace app\controller;

use app\library\Auth;
use fast\Random;

class User extends BackendController{


    public function _initialize()
    {
        //parent::_initialize();
        $this->model = new \app\model\User();
    }

    public function after_index($lists){
        foreach ($lists as &$list) {
            unset($list['password']);
            unset($list['salt']);
        }
        return $lists;
    }

    public function after_add($params){
        $auth=new Auth();
        $date['salt']=Random::alnum(6);
        $date['password']=$auth->getEncryptPassword($params['password'],$date['salt']);
        $date['status']='normal';
        $this->model->where('id',$params['id'])->save($date);
    }

    public function edit($id = null)   {
        //return parent::edit($id); // TODO: Change the autogenerated stub
    }

    public function change_password($old_pass,$new_pass){
        $user=$this->model->find($this->auth->id);
        if($user){
            if($this->auth->getEncryptPassword($old_pass,$user['salt'])==$user['password']){
                $user->password=$this->auth->getEncryptPassword($new_pass,$user['salt']);
                $user->save();
                $this->success('修改成功');
            }else{
                $this->error('密码不正确');
            }
        }else{
            $this->error('无此用户');
        }
    }
}